var prevWidth = 0;
var formTabManager = null;
var url_modified = false;

function save_code()
{
	$('form_element').sendPhpr('onSave', {
		prepareFunction: function(){phprTriggerSave();}, 
		extraFields: {redirect: 0}, 
		loadIndicator: {show: false}, 
		onBeforePost: LightLoadingIndicator.show.pass('Saving...'), 
		onComplete: LightLoadingIndicator.hide, 
		onFailure: popupAjaxError,
		onAfterUpdate: setupInfoSize,
		onAfterError: setupInfoSize,
		onBeforeScriptEval: function(){UnloadManager.reset_changes()},
		update: $('form_element').getElement('div.formFlash')}); 

	return false;
}

function resizeEditors()
{
	// $('form_element').getElements('li.html textarea').each(function(ta_element){
	// 	phprTriggerSave();
	// 	tinyMCE.execCommand('mceRemoveControl',false,ta_element.id);
	// })
	// 
	// $('form_element').getElements('li.html textarea').each(function(ta_element){
	// 	tinyMCE.execCommand('mceAddEditor',false,ta_element.id);
	// 	assignEditorEvents(ta_element.id);
	// })
}

window.addEvent('onTabManagerAdded', function(manager){
	if (manager.tabs_element.id != 'sidebar_tabs')
	{
		formTabManager = manager;
		
		manager.addEvent('onTabClick', function(tab, page){
			// $('form_element').getElements('li.html textarea').each(function(ta_element){
			// 	if (phpr_field_initialized.has(ta_element.id))
			// 	{
			// 		phprTriggerSave();
			// 		tinyMCE.execCommand('mceRemoveControl',false,ta_element.id);
			// 		tinyMCE.execCommand('mceAddEditor',false,ta_element.id);
			// 	}
			// })
			
			if ($('sidebar_tabs'))
				(function(){setupInfoSize();}).delay(100);
		})
	}
})

function setupInfoSize()
{
	if (!$('form_element'))
		return;
	
	var formElement = $('form_element').getElement('div.form');
	var formSize = formElement.getSize();
	var tabElement = $('sidebar_tabs').getElement('li');
	var tabSize = tabElement.getSize();

	var tabHeight = formSize.y-23-tabSize.y;
	$('sidebar_tabs').getElements('li.page').each(function(element){
		element.setStyle('height', tabHeight+'px');
	})
	
	var filesHeaderSize = $('files_header').getSize();
	var filesStatusBarSize = $('file_browser_statusbar').getSize();
	var filesControlsBarSize = $('file_browser_controls').getSize();
	
	var filesDescriptionSize = $('files_description').getSize();
	$('file_browser_file_list').setStyle('height', (tabHeight-filesHeaderSize.y-filesDescriptionSize.y - 4 - filesStatusBarSize.y - filesControlsBarSize.y)+'px');
}

window.addEvent('domready', function(){
	$(document.getElement('html')).bindKeys({
		'meta+s, ctrl+s': save_code
	});
	
	prevWidth = window.getSize().x;
	
	initSidebarTabManager();
	(function(){setupInfoSize();}).delay(600);
	
	var title_field = $('Blog_Post_title');
	if (title_field && $('new_record_flag'))
	{
		title_field.addEvent('keyup', update_url_title.pass(title_field));
		title_field.addEvent('change', update_url_title.pass(title_field));
		title_field.addEvent('paste', update_url_title.pass(title_field));
	}
	
	if ($('new_record_flag'))
	{
		var url_element = $('Blog_Post_url_title');
		url_element.addEvent('change', function(){url_modified=true;});
	}
});

function initSidebarTabManager()
{
	if ($('sidebar_tabs'))
		new TabManager('sidebar_tabs', 'sidebar_pages', {trackTab: false});
}

window.addEvent('resize', function() {
	if (prevWidth == window.getSize().x)
		return;

	resizeEditors();
	prevWidth = window.getSize().x;
});

function assignEditorEvents(editor)
{
	if (!editor || !tinyMCE.get(editor))
		return;
	
	var container = $(tinyMCE.get(editor).getContainer());
	if (container)
	{
		var glyph = container.getElement('a.mceResize');
		if (glyph)
		{
			glyph.addEvent('mousemove', setupInfoSize);
			glyph.addEvent('mouseout', setupInfoSize);
		}
		
		var statusbar = container.getElement('td.mceStatusbar');
		if (statusbar)
			statusbar.addEvent('mouseout', setupInfoSize);
	}
}

function update_url_title(field_element)
{
	if (!url_modified)
		$('Blog_Post_url_title').value =  convert_text_to_url(field_element.value);
}

window.addEvent('phpreditoradded', assignEditorEvents)